﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YasharEl.Infrastructure.EntityModel.Configuration
{
    public partial class ConnectionStringNameExistsCallbackValidatorClass
    {
        public static void ConnectionStringNameExists(object value)
        {
            if( value == null )
                return;

            if( string.IsNullOrEmpty(value.ToString()))
                return;

            if (System.Configuration.ConfigurationManager.ConnectionStrings == null ||
                System.Configuration.ConfigurationManager.ConnectionStrings.Count == 0)
            {
                throw new InvalidSystemConfigurationException("There is not defined connection strings in configuration file.");
            }

            foreach (System.Configuration.ConnectionStringSettings conStringSettings in System.Configuration.ConfigurationManager.ConnectionStrings)
            {
                if (conStringSettings.Name == value.ToString())
                {
                    return;
                }
            }

            throw new InvalidSystemConfigurationException(
                string.Format("Connection string with name {0} not defined in configuration file.", value.ToString())
            );
        }
    }
}
